<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()

// 表单数据
const formData = reactive({
    username: '',
    password: ''
})

// 表单验证状态
const formValidation = reactive({
    username: {
        error: false,
        message: ''
    },
    password: {
        error: false,
        message: ''
    }
})

// 密码可见性状态
const showPassword = ref(false)

// 按钮加载状态
const loading = ref(false)

// 用户名验证
const validateUsername = () => {
    if (!formData.username) {
        formValidation.username.error = true
        formValidation.username.message = '请输入用户名'
        return false
    }
    if (formData.username.length < 4 || formData.username.length > 20) {
        formValidation.username.error = true
        formValidation.username.message = '用户名长度应在4-20个字符之间'
        return false
    }
    formValidation.username.error = false
    formValidation.username.message = ''
    return true
}

// 密码验证
const validatePassword = () => {
    if (!formData.password) {
        formValidation.password.error = true
        formValidation.password.message = '请输入密码'
        return false
    }
    if (formData.password.length < 6) {
        formValidation.password.error = true
        formValidation.password.message = '密码长度不能少于6个字符'
        return false
    }
    formValidation.password.error = false
    formValidation.password.message = ''
    return true
}

// 表单提交
const handleSubmit = async () => {
    const isUsernameValid = validateUsername()
    const isPasswordValid = validatePassword()

    if (!isUsernameValid || !isPasswordValid) {
        return
    }

    loading.value = true
    try {
        // 模拟登录请求
        await new Promise(resolve => setTimeout(resolve, 1500))
        // 设置登录状态
        localStorage.setItem('isLoggedIn', 'true')
        // 跳转到首页
        router.push('/')
    } catch (error) {
        alert('登录失败，请重试')
    } finally {
        loading.value = false
    }
}

// 切换密码可见性
const togglePasswordVisibility = () => {
    showPassword.value = !showPassword.value
}
</script>

<template>
    <div class="login-container">
        <div class="login-box">
            <h2>用户登录</h2>

            <div class="form-item">
                <label>用户名</label>
                <input type="text" v-model="formData.username" placeholder="请输入用户名" @blur="validateUsername"
                    :class="{ 'error': formValidation.username.error }">
                <span class="error-message" v-if="formValidation.username.error">
                    {{ formValidation.username.message }}
                </span>
            </div>

            <div class="form-item">
                <label>密码</label>
                <div class="password-input">
                    <input :type="showPassword ? 'text' : 'password'" v-model="formData.password" placeholder="请输入密码"
                        @blur="validatePassword" :class="{ 'error': formValidation.password.error }">
                    <button type="button" class="toggle-password" @click="togglePasswordVisibility">
                        {{ showPassword ? '隐藏' : '显示' }}
                    </button>
                </div>
                <span class="error-message" v-if="formValidation.password.error">
                    {{ formValidation.password.message }}
                </span>
            </div>

            <button class="submit-button" @click="handleSubmit" :disabled="loading">
                {{ loading ? '登录中...' : '登录' }}
            </button>
        </div>
    </div>
</template>

<style scoped>
.login-container {
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    background-color: #f5f5f5;
    padding: 20px;
}

.login-box {
    background: white;
    padding: 30px;
    border-radius: 8px;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
    width: 100%;
    max-width: 400px;
}

h2 {
    text-align: center;
    color: #333;
    margin-bottom: 30px;
    font-weight: 500;
}

.form-item {
    margin-bottom: 20px;
}

label {
    display: block;
    margin-bottom: 8px;
    color: #333;
    font-size: 14px;
}

input {
    width: 100%;
    padding: 10px 12px;
    border: 1px solid #dcdfe6;
    border-radius: 4px;
    font-size: 14px;
    transition: all 0.3s;
}

input:focus {
    outline: none;
    border-color: #409eff;
    box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.2);
}

input.error {
    border-color: #f56c6c;
}

input.error:focus {
    box-shadow: 0 0 0 2px rgba(245, 108, 108, 0.2);
}

.password-input {
    position: relative;
}

.toggle-password {
    position: absolute;
    right: 10px;
    top: 50%;
    transform: translateY(-50%);
    background: none;
    border: none;
    color: #606266;
    cursor: pointer;
    padding: 4px 8px;
    font-size: 14px;
}

.toggle-password:hover {
    color: #409eff;
}

.error-message {
    display: block;
    color: #f56c6c;
    font-size: 12px;
    margin-top: 6px;
}

.submit-button {
    width: 100%;
    padding: 12px 20px;
    background-color: #409eff;
    color: white;
    border: none;
    border-radius: 4px;
    font-size: 16px;
    cursor: pointer;
    transition: background-color 0.3s;
}

.submit-button:hover {
    background-color: #66b1ff;
}

.submit-button:disabled {
    background-color: #a0cfff;
    cursor: not-allowed;
}
</style>